var currentAnnot = -1;
var toSpawn;

function spawnAnnotation() {
	//Sets the index of the annotation to spawn
	$("#pinsAnnot > img").each(findAnnotationToSpawn);

	if (currentAnnot != toSpawn) {
		//Set the annotation
		openAnnotationInList(parseInt(toSpawn));
		//Sets currentAnnotation to the right index
		currentAnnot = toSpawn;

		//Spawns a tooltip with the title of the annotation
		$("img#" + toSpawn).qtip('show');
		//hide after a 3 seconds
		setTimeout(function() {
			$("img#" + toSpawn).qtip('hide')
		}, 3000);
	}
}

function openAnnotationInList(id) {
	$("#accordion").accordion('option', 'active', id);
}

function findAnnotationToSpawn() {
	var video = document.getElementById("video");
	var videoTime = parseInt(video.currentTime);

	if ($(this).data('timeannot')-1 < videoTime) {
		var id = $(this).attr('id');
		toSpawn = id;
	}
}

function initToolTips() {
	$("#pinsAnnot > img").each(createToolTip);
}

function createToolTip() {
	var img = $(this);
	var id = img.attr('id');
	img.qtip({
		id : 'bubbleAnnot' + id,
		content : {
			text : $("#accordion h3").eq(id).text()
		},
		position : {
			my : 'bottom center',
			at : 'top center',
			target : img
		},
		show : 'mouseover',
		hide : 'mouseout',

	});
}

function initAddAnnotation(id) {
	$("#addAnnotationForm").submit(function() {
		addAnnotationCall(id);
		return false;
	});
}

function addAnnotationCall(id, idU, status) {

	var success;
	var inputEditable = document.getElementById("editable");
	var inputShared = document.getElementById("shared");
	var timeVideo = document.getElementById("video").currentTime;
	if (inputEditable != null) {
		$.ajax({
			url : "addAnnotationForm.php",
			data : {
				title : $('#title').val(),
				time : timeVideo,
				content : $('#newContent').val(),
				shared : inputShared.checked,
				editable : inputEditable.checked,
				idVideo : id,
				idUser : idU
			},
			async : false,
			dataType : "text"
		}).done(function(res) {
			success = parseInt(res);
		});
	} else {
		$.ajax({
			url : "addAnnotationForm.php",
			data : {
				title : $('#title').val(),
				time : timeVideo,
				content : $('#newContent').val(),
				shared : inputShared.checked,
				idVideo : id,
				idUser : idU
			},
			async : false,
			dataType : "text"
		}).done(function(res) {
			success = parseInt(res);
		});
	}

	if (success != 0) {
		var contentString = "";
		contentString += '<p>The Annotation was successfully added. It will appear on the next refresh of the page.</p>';
		contentString += '<form id="addAnnotationForm" onSubmit="return addAnnotationCall(' + id + ',' + idU + ','+status+ ');" method="POST">';
		contentString += '<p>Title : <input type="text" name="title" id="title"/></p>';
		contentString += '<p>Time :Current video time</p>';
		contentString += '<p>Content : <textarea name="newContent" id="newContent" rows=3 cols=30 ></textarea></p>';
		contentString += '<p><input name="shared" id="shared" type="checkbox" checked="yes"/> shared</p>';
		if (status == 2) {
			contentString += '<p><input name="editable" id="editable" type="checkbox" checked="yes"/> editable</p>';
		}
		contentString += '<input type="submit"/>';
		contentString += '</form>';
		$("#addAnnotation").html(contentString);

	} else {
		alert("fail to add annotation !");
	}
	return false;
}
